package com.cy.pj.common.Aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SysCacheAspect {
	@Autowired
	private SimpleCache simpleCache;
	
	@Pointcut("@annotation(com.cy.pj.common.Aspect.RequiredCache)")
	public void doCachePointCut() {}
	
	
	@Pointcut("@annotation(com.cy.pj.common.Aspect.RequiredCache2)")
	public void C() {}
	@Around("C()")
	public Object B(ProceedingJoinPoint jp)throws Throwable {
		Object result=jp.proceed();
		simpleCache.clearObject();
		return result;
	}
	
	
	@Around("doCachePointCut()")
	public Object around(ProceedingJoinPoint jp)throws Throwable{
		System.out.println("Get data from cache");
		Object obj = simpleCache.getObject("deptCache");
		if(obj!=null)return obj;
		Object result=jp.proceed();
		System.out.println("put data to cache");
		simpleCache.putobject("deptCache",result);
		return result;
	}
}	
